package com.jinlejia.bbs.logic;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import static com.jinlejia.bbs.http.utils.HttpUtils.JsonParse;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;
/**
 * 删除 话题，回答，评论
 */

public class DeleteLogic {
    private final Context context;
    public DeleteLogic(Context context){
        this.context = context;
    }


    public void deleteTopic(final String topicCode,final CallBack callBack){
        JSONObject json = new JSONObject();
        json.put("topicCode",topicCode);
        httpCallForm(BBSConfig.delTopic,json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onError(Exception e) {
                        callBack.onResult(false,topicCode);
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        boolean result = parse.parse(Boolean.class);
                        callBack.onResult(result,topicCode);
                    }

                    @Override
                    public void onNoResponse(int code) {
                        callBack.onResult(false,topicCode);
                    }
                }));
    }


    public void deleteAnswer(final String answerCode, final CallBack callBack){
        JSONObject json = new JSONObject();
        json.put("answerCode",answerCode);
        httpCallForm(BBSConfig.delAnswer,json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onError(Exception e) {
                        callBack.onResult(false,answerCode);
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        boolean result = parse.parse(Boolean.class);
                        callBack.onResult(result,answerCode);
                    }

                    @Override
                    public void onNoResponse(int code) {
                        callBack.onResult(false,answerCode);
                    }
                }));
    }

    public void deleteComment(final String commentCode,final CallBack callBack){
        JSONObject json = new JSONObject();
        json.put("commentsCode",commentCode);
        httpCallForm(BBSConfig.delComments,json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onError(Exception e) {
                        callBack.onResult(false,commentCode);
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        boolean result = parse.parse(Boolean.class);
                        callBack.onResult(result,commentCode);
                    }

                    @Override
                    public void onNoResponse(int code) {
                        callBack.onResult(false,commentCode);
                    }
                }));
    }

    public interface CallBack{
        void onResult(boolean result,String code);
    }
}
